
export namespace DataSync.Filter {
    interface IDataFilter {
        FormatData(data: any): any;
        FilterName: string;
    }

    class StoreDataFilter implements IDataFilter {
        FormatData(data: any) {
            let barcodeIndex = data.configs.findIndex(x => x.key == "scanner.barcode.format");
            if (barcodeIndex > -1) {
                data.configs[barcodeIndex].value.mode = JSON.parse(data.configs[barcodeIndex].value.mode);
            }

            return data;
        }
        get FilterName(): string {
            return "Store";
        }
    }

    class DeskTableDataFilter implements IDataFilter {
        FormatData(data: any) {
            delete data.status;
            delete data.lock;
            if (!data.feeRuleId) data.feeRuleId = "";
            return data;
        }
        get FilterName(): string {
            return "DeskTable";
        }
    }

    class TablePartyDataFilter implements IDataFilter {
        FormatData(data: any) {
            delete data.status;
            delete data.closedTime;
            return data;
        }
        get FilterName(): string {
            return "TableParty";
        }
    }
    export class DataSyncFilterManager {
        static Current: DataSyncFilterManager = new DataSyncFilterManager();
        private filterList: IDataFilter[] = [];
        constructor() {
            this.AddFilter(new StoreDataFilter());
            this.AddFilter(new DeskTableDataFilter());
            this.AddFilter(new TablePartyDataFilter());
        }

        Filter<T>(FilterName: string, data: T): T {
            let filter = this.filterList.find(x => x.FilterName == FilterName);
            if (filter) {
                return filter.FormatData(data);
            }
            else {
                return data;
            }
        }

        AddFilter(filter: IDataFilter) {
            let f = this.filterList.find(x => x.FilterName == filter.FilterName);
            if (!f) this.filterList.push(filter);
        }

        Destory() {
            this.filterList = [];
        }
    }
}